package com.example.mybaseapp.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.app.ActivityCompat
import androidx.core.view.LayoutInflaterCompat
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.example.mybaseapp.R
import com.example.mybaseapp.util.ViewPagerTabLayoutUtil
import com.example.mybaseapp.vt.BlankFragment
import com.example.mybaseapp.vt.VTAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

class ViewPagerActivity : AppCompatActivity() {
    private val titleNames = arrayListOf("界面1","界面2","界面3","界面4")
    private val fragments = arrayListOf<Fragment>(
        BlankFragment.newInstance("界面1","界面1"),
        BlankFragment.newInstance("界面2","界面1"),
        BlankFragment.newInstance("界面3","界面1"),
        BlankFragment.newInstance("界面4","界面1"))
    private lateinit var tabLayout:TabLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_view_pager)
//        initViewPager()
        initPager()
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
    }

    private fun initPager() {
        tabLayout = findViewById<TabLayout>(R.id.tabLayout)
        val viewPager2 = findViewById<ViewPager2>(R.id.viewpager2)
        viewPager2.adapter = VTAdapter(this,fragments)
        ViewPagerTabLayoutUtil().contactViewPagerWithTabLayout(viewPager2,tabLayout,
            object : ViewPagerTabLayoutUtil.ViewPagerTabLayoutListener {
                override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
                    val view = LayoutInflater.from(this@ViewPagerActivity).inflate(R.layout.item_tab, null) as ConstraintLayout
                    view.findViewById<TextView>(R.id.tx).text = titleNames[position]
                    tab.customView = view
                }

                override fun onTabSelected(tab: TabLayout.Tab) {
                    val customView = tab?.customView?.findViewById<TextView>(R.id.tx)!!
                    customView.setTextColor(ActivityCompat.getColor(this@ViewPagerActivity, R.color.select_color))
//                    customView.text = "选中了"
                }

                override fun onTabUnselected(tab: TabLayout.Tab) {
                    val textView = tab?.customView?.findViewById<TextView>(R.id.tx)!!
                    textView.setTextColor(ActivityCompat.getColor(this@ViewPagerActivity, R.color.black))
                    textView.text = titleNames[tab.position ?:0]
                }

                override fun onTabReselected(tab: TabLayout.Tab) {
                }

            })
    }

    private fun initViewPager() {
        tabLayout = findViewById<TabLayout>(R.id.tabLayout).apply {
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    val customView = tab?.customView?.findViewById<TextView>(R.id.tx)!!
                    customView.setTextColor(ActivityCompat.getColor(this@ViewPagerActivity, R.color.select_color))
                    customView.text = "选中了"
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    val textView = tab?.customView?.findViewById<TextView>(R.id.tx)!!
                    textView.setTextColor(ActivityCompat.getColor(this@ViewPagerActivity, R.color.black))
                    textView.text = titleNames[tab.position ?:0]
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }

            })
        }
        val viewPager2 = findViewById<ViewPager2>(R.id.viewpager2).apply {
            adapter = VTAdapter(this@ViewPagerActivity, fragments)
            currentItem = 0
            //禁用预加载
            setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT)
        }

        TabLayoutMediator(tabLayout, viewPager2
        ) { tab, position ->
            val view = LayoutInflater.from(this).inflate(R.layout.item_tab, null) as ConstraintLayout
            view.findViewById<TextView>(R.id.tx).text = titleNames[position]
            tab.customView = view

//            这里可以针对不同的icon，进行手动处理
//            when(position){
//                0 -> {
//                   tab.setCustomView(R.layout.item_tab)
//                    (tab.customView as TextView).apply {
//                        text = titleNames[0]
//                        setTextColor(ActivityCompat.getColor(this@ViewPagerActivity, R.color.select_color))
//                    }
//                }
//                1 -> {
//                    val inflate = LayoutInflater.from(this).inflate(R.layout.item_tab, null)
//                    tab.setCustomView(R.layout.item_tab)
//                    (tab.customView as TextView).apply {
//                        text = titleNames[1]
//                    }
//                }
//                2 -> {
//                    val inflate = LayoutInflater.from(this).inflate(R.layout.item_tab, null)
//                    tab.setCustomView(R.layout.item_tab)
//                    (tab.customView as TextView).apply {
//                        text = titleNames[2]
//                    }
//                }
//                else -> {
//                    val inflate = LayoutInflater.from(this).inflate(R.layout.item_tab, null)
//                    (inflate as TextView).apply {
//                        text = titleNames[3]
//                    }
//                    tab.setCustomView(inflate)
//                }
//            }
        }.attach()
    }

    override fun onDestroy() {
        super.onDestroy()
    }
}